const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

//处理闹钟 和 时间
const update_fun_timer = n => {
  let timeDate = new Date();
  let { alarm_list,bleTx,bleStore,music_ring } = app.store.getState();
  if (n == 0) {
    bleTx[0] = 0;
    bleTx[1] = timeDate.getFullYear() - 2000;
    bleTx[2] = timeDate.getMonth() + 1;
    bleTx[3] = timeDate.getDate();
    bleTx[4] = timeDate.getHours();
    bleTx[5] = timeDate.getMinutes();
    bleTx[6] = timeDate.getSeconds();
  }else if (n > 0 && n <= 3) {
    bleTx[0] = n;
    bleTx[1] = alarm_list[n - 1].alarm_tm_hour1;
    bleTx[2] = alarm_list[n - 1].alarm_tm_min1;
    bleTx[3] = alarm_list[n - 1].alarm_state1;
    bleTx[4] = alarm_list[n - 1].alarm_play_time1;
    bleTx[5] = alarm_list[n - 1].alarm_ring_tone1;
    bleTx[6] = alarm_list[n - 1].alarm_over_sleeping1;
  }else{
    //清空一下数据包
    for (let index = 1; index < bleTx.length; index++) {
        bleTx[index] = 0;
    }
    //其他设置闹钟参数处理
    bleTx[0] = n;
    switch (n) {
      case 8://开关灯状态
        bleTx[1] = bleStore[2];
        break;
      case 9://灯光亮度
        bleTx[1] = bleStore[3];
        break;
      case 10://延迟关灯状态
        bleTx[1] = bleStore[4];
        bleTx[2] = bleStore[5];
        break;
      case 11://延迟关灯时间
        bleTx[1] = bleStore[4];
        bleTx[2] = bleStore[5];
        break;
      case 12://是否12小时制
        bleTx[1] = bleStore[6];
        break;
      case 13://整时报点
        bleTx[1] = bleStore[7];
        break;
      case 14://温度单位
        bleTx[1] = bleStore[1];
        break;
      case 15://音量
        bleTx[1] = bleStore[8];
        break;
      case 16:// 铃声渐强
        bleTx[1] = bleStore[9];
        break;
      case 17:// 倒计时
        bleTx[1] = bleStore[10];
        bleTx[2] = bleStore[11];
        break;
      case 18:// 倒计时时间
        bleTx[1] = bleStore[10];
        bleTx[2] = bleStore[11];
        break;
      case 19:// 正计时
        bleTx[1] = bleStore[12];
        bleTx[2] = bleStore[13];
        break;
      case 20:// 正计时时间
        bleTx[1] = bleStore[12];
        bleTx[2] = bleStore[13];
        break;
      case 21://闹钟响铃呼吸灯
        bleTx[1] = bleStore[14];
        break;
      case 23://闹钟响铃呼吸灯
        bleTx[1] = music_ring;
        break;
      default:
        break;
    }
    
  }
  app.store.setState({ bleTx});
}


const app = getApp()

//发送数据给蓝牙设备
/*data 传0 发时时间  传1-3发送修改的闹钟 传4-6 查询闹钟1 2 3的数据  传7一次查询所有参数不包含闹钟 */
const ble_send_date = data => {

    update_fun_timer(data);//更新发送数据包的 闹钟/时间

    let { bleTx,bleUser } = app.store.getState();
    console.log(bleTx);
    let buffer = new ArrayBuffer(10); //申请数组长度
    let dataView = new DataView(buffer);
    for (let index = 0; index < bleTx.length; index++) {
      dataView.setInt8(index, bleTx[index]);
    }
    let deviceId = bleUser.deviceId;
    let serviceId = bleUser.serviceId;
    let characteristicId = bleUser.characteristicId;
    wx.writeBLECharacteristicValue({
      deviceId,
      serviceId,
      characteristicId,
      value: buffer,
      success: () => {
        console.log('[蓝牙发送] 发送成功');
      },
      fail: () => {
        console.log('[蓝牙发送] 发送失败');
        wx.showToast({
          title: '发送失败',
          icon: 'none'
        });
      }
    });
}

module.exports = {
  formatTime,
  ble_send_date
}
